// pages/renlian/renlian.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    msg:"",
    scrollTop:0,
    newslist:[{
      id:1,
      username:"华小清",
      photo:"https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1859067755,2869084751&fm=26&gp=0.jpg",
      content:"你好啊",
      type:"txt"
    }]

  },
  // myinput:function(event){
  //   console.log(event.detail.value)
  //   this.setData({
  //     msg:event.detail.value
  //   })
  // },
  // send:function(){
  //   let that=this
  //   if(!that.data.msg){
  //     wx.showToast({
  //       title:'消息不能为空',
  //     })
  //     return;
  //   }
  //   else{
  //   let obj={
  //     id:that.data.newslist.lenght+1,
  //     username:that.data.userInfo.nickName,
  //     photo:that.data.userInfo.avatarUrl,
  //     content:that.data.msg,
  //     type:"txt"
  //    }
  //    that.setData({
  //      newslist:that.data.newslist.concat(obj)
  //    })
    //  wx.request({
    //    url:'http://www.thd.cool:8000/chat',
    //    method:"GET",
    //    data:{
    //      question:that.data.msg
    //    },
    //    success:function(res){
    //      let answerObj={
    //      id:that.data.newslist.length+1,
    //      username:"华小清",
    //      photo:"https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1859067755,2869084751&fm=26&gp=0.jpg",
    //      content:res.data.result.data.answer,
    //      type:"txt"
    //     }
    //     that.setData({
    //       newslist:that.data.newslist.concat(answerObj),
    //       msg:"",
    //       scrollTop:that.data.scrollTop+100
    //     });
        

    //      }
    //  })
    // }  
    // },
  upload:function(){
    let that =this;
    wx.chooseImage({
      count:1,
     sizeType: ['original' ,'compressed'],
     sourceType: ['album', 'camera'],
     success :function(res){
     console.log(res)
     let imgObj={
       id:that.data.newslist.length+1,
       username:that.data.userInfo.nickName,
       photo:that.data.userInfo.avatarUrl,
       content:res.tempFilePaths[0],
       type:"img"


     }
     that.setData({
       newslist:that.data.newslist.concat(imgObj)
     });
     wx.uploadFile({
      filePath:res.tempFilePaths[0],
      name: 'file',
      url: 'http://www.thd.cool:8000/face',
      header:{
          "Content-Type":"form-data"
      },
      success:function(mydata){
          console.log(mydata)
          let jsonObj = JSON.parse(mydata.data);
          let age = jsonObj.faceinfo.age;
          let beauty = jsonObj.faceinfo.beauty;
          let smile = "";
          if(jsonObj.faceinfo.expression > 50){
              smile = "哈哈大笑"
          }else{
              smile = "微笑"
          }
          let sex = "";
          if(jsonObj.faceinfo.gender > 50){
              sex = "男";
          }else{
              sex = "女";
          }
          let glass = "";
          if(jsonObj.faceinfo.glass == 0){
              glass = "没有戴眼镜"
          }else{
              glass = "戴眼镜的你真帅"
          }

          let faceInfo =  `面部识别结果是：
                         性别：${sex} 
                         年龄:${age}
                       魅力值:${beauty}
                     面部表情:${smile}
                     ${glass}`
          let msgObj = {
              id:that.data.newslist.length + 1,
              username:"华小清",
              photo:"https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1859067755,2869084751&fm=26&gp=0.jpg",
              content:faceInfo,
              type:"txt"
          }
          that.setData({
              newslist:that.data.newslist.concat(msgObj),
              scrollTop:that.data.scrollTop +400
          })
      }
    })

     }


    })
    
  


    },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.userInfo)
    this.setData({
      userInfo:app.globalData.userInfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})